package com.nc.delivery_project.server.service;

import com.nc.delivery_project.server.FillDb;
import com.nc.delivery_project.server.dao.AbstractDao;
import com.nc.delivery_project.server.dao.ScheduleDao;
import com.nc.delivery_project.server.domain.Currency;
import com.nc.delivery_project.server.domain.Department;
import com.nc.delivery_project.server.domain.LegalPerson;
import com.nc.delivery_project.server.domain.Schedule;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by miroshnik on 1/28/2015.
 * Edition by DRAGON 28.01.2015
 */
public class ScheduleService extends BaseService<Schedule> {
    public static void main(String[] args) {
        ScheduleService service = new ScheduleService();

        List<Schedule> schedules = service.read();
        System.out.println(schedules);
    }

    public ScheduleService() {
        this(new ScheduleDao());
    }

    public ScheduleService(Connection connection, boolean isAutoCloseConnection){
        super(new ScheduleDao(), connection, isAutoCloseConnection);
    }

    public ScheduleService(AbstractDao < Schedule > scheduleDao) {
        super(scheduleDao);
    }

    @Override
    public Schedule read(int id){
        Schedule schedule = null;

        try {
            Connection conn = getConnection();
            dao.setConnection(conn);
            schedule = dao.read(id);

            if (schedule != null) {
                Department origin = new DepartmentService(conn, false).read(schedule.getOrigin().getDeptId());
                if (origin != null) {
                    schedule.setOrigin(origin);
                }

                Department dest = new DepartmentService(conn, false).read(schedule.getDestination().getDeptId());
                if (dest != null) {
                    schedule.setDestination(dest);
                }

                LegalPerson scheduleLegalPerson = new LegalPersonService(conn, false).read(schedule.getTransportCompany().getLegalPersonId());
                if (scheduleLegalPerson != null) {
                    schedule.setTransportCompany(scheduleLegalPerson);
                }
                Currency currency = new CurrencyService(conn, false).read(schedule.getCurrency().getId());
                if (currency != null) {
                    schedule.setCurrency(currency);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return schedule;
    }

    @Override
    public List<Schedule> read(){
        List<Schedule> schedules = null;

        try {
            Connection conn = getConnection();
            dao.setConnection(conn);
            schedules = dao.read();

            DepartmentService service = new DepartmentService(conn, false);
            LegalPersonService legalPersonService = new LegalPersonService(conn, false);
            CurrencyService currencyService = new CurrencyService(conn, false);

            for (Schedule schedule : schedules) {
                schedule.setDestination(service.read(schedule.getOrigin().getDeptId()));
                schedule.setOrigin(service.read(schedule.getDestination().getDeptId()));
                schedule.setTransportCompany(legalPersonService.read(schedule.getTransportCompany().getLegalPersonId()));
                schedule.setCurrency(currencyService.read(schedule.getCurrency().getId()));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return schedules;
    }

    public List<Schedule> read(Date startDate, Date endDate) {
  //     List<Schedule> scheduleList = read();

        List<Schedule> scheduleList = FillDb.createSchedules(500, false);

        List<Schedule> filteredList = new ArrayList<>();
        for (Schedule schedule : scheduleList) {
            if (schedule.getDepartureDate().compareTo(startDate) >= 0
                    && schedule.getArrivalDate().compareTo(endDate) <= 0) {
                filteredList.add(schedule);
            }
        }

        return filteredList;
    }
}
